<?php
/**
 * TomatoCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version 	$Id: list.phtml 4684 2010-08-16 08:44:14Z huuphuoc $
 * @since		2.0.0
 */
?>
<?php
$this->headTitle()->prepend($this->translate('about_product_description') . ' - ');
$this->headScript()->appendFile($this->APP_STATIC_SERVER . '/js/jquery.json-2.3.min.js');
?>
<script type="text/javascript">
    $(document).ready(function(){
        //delete Actin
        $(".deleteAction").click(function(){
            if(confirm("<?php echo $this->translate('product_list_action_delete_confirm') ?>")){
                var id = $(this).attr("rel");
                $.ajax({
                    type: 'POST',
                    url: "<?php echo $this->url(array(), 'product_product_delete'); ?>",
                    data: { id: id },
                    success: function(response) {
                        if ('RESULT_OK' == response) {
                            $('#product_' + id).fadeOut('slow');
                        }
                    }
                });
            }
            return false;
        });
        //xóa
        $("#btnDelete").click(function(){
            var arrChkId = $(".chkId");
            var ids = new Array();
            for(var i =0; i < arrChkId.length; i++){
                if(arrChkId.eq(i).attr("checked")){
                    ids.push(arrChkId.eq(i).val());
                }
            }
            if(ids.length > 0){
                if(confirm("<?php echo $this->translate('product_list_action_delete_many_confirm') ?>")){
                    $.ajax({
                        type: 'POST',
                        url: "<?php echo $this->url(array(), 'product_product_delete'); ?>",
                        data: { id: $.toJSON(ids) },
                        success: function(response) {
                            if ('RESULT_OK' == response) {
                                for(var i in ids)
                                    $('#product_' + ids[i]).fadeOut('slow');
                            }
                        }
                    });
                }else{
                    return false;
                }
            }else{
                alert("<?php echo $this->translate('product_list_bulk_action_none_selected') ?>");
                return false;
            }
        });
    });
</script>
<div id="r-heading">
    <img src="<?php echo $this->APP_STATIC_SERVER . '/skins/admin/default/images/product.png'; ?>" alt=""/>
    <span><?php echo $this->translate('product_list_page_title') ?></span>
</div>
<?php
//nếu có message thì hiển thị
$mesage = $this->flashMessenger();
if ($mesage) {
    echo '<div id="message">' . $mesage . '</div>';
}
?>
<div>
    <form id="frmActions" action="" method="post">
        <div id="actions">
              <!--<input type="text" id="txtKeyword" name="keyword" class="text" value="" size="25"/>
              <input type="submit" id="btnSearch" class="button" value="Tìm kiếm"/>  -->
            <input type="button" id="btnCreate" class="button" value="<?php echo $this->translate('permission_product_add') ?>" onclick="location = '<?php echo $this->url(array(), 'product_product_add'); ?>';"/>
            <input type="button" id="btnDelete" class="button" value="<?php echo $this->translate('product_list_delete_selected') ?>"/>
        </div>
        <div id="filters">
                  <div class="f-data">
                      <label><?php echo $this->translate('product_list_search_keyword'); ?>:</label>
                      <input type="text" style="width: 180px" name="keyword" value="<?php if (isset($this->exp['keyword'])) : ?><?php echo $this->exp['keyword']; ?><?php endif; ?>" />
                  </div>
                  <div class="f-data">
                        <label><?php echo $this->translate('product_list_search_id'); ?>:</label>
				<input type="text" style="width: 180px" name="productId" id="productId" value="<?php if (isset($this->exp['id'])) : ?><?php echo $this->exp['id']; ?><?php endif; ?>" />
                  </div>
                  <div class="f-data">
                        <label><?php echo $this->translate('product_language'); ?>:</label>
				<?php echo $this->languageSelector(); ?>
                  </div>
                  <div class="f-data" >
                        <label><?php echo $this->translate('product_list_search_category'); ?>:</label>
                        <div class="translatable">
                        <?php
                        echo $this->helperLoader('category')->categorySelect(array(
																'id'       => 'category',
																'name'     => 'category',
																'selected' => (isset($this->exp['category_id']) ? $this->exp['category_id'] : null)
															), null, 'product');
                        ?>
                            </div>
                  </div>
                  <div class="f-data">
                        <label><?php echo $this->translator('product_list_search_status'); ?>:</label>
				<select name="status" style="width: 180px">
					<option value=""<?php if (!isset($this->exp['status'])) : ?> selected="selected"<?php endif; ?>>---</option>
					<option value="actived"<?php if (isset($this->exp['status']) && $this->exp['status'] == 'actived') : ?> selected="selected"<?php endif; ?>><?php echo $this->translate('product_list_status_actived'); ?></option>
					<option value="inactived"<?php if (isset($this->exp['status']) && $this->exp['status'] == 'inactived') : ?> selected="selected"<?php endif; ?>><?php echo $this->translate('product_list_status_inactived'); ?></option>
					<option value="drafted"<?php if (isset($this->exp['status']) && $this->exp['status'] == 'drafted') : ?> selected="selected"<?php endif; ?>><?php echo $this->translate('product_list_status_drafted'); ?></option>
					<option value="deleted"<?php if (isset($this->exp['status']) && $this->exp['status'] == 'deleted') : ?> selected="selected"<?php endif; ?>><?php echo $this->translate('product_list_status_deleted'); ?></option>
				</select>
                  </div>
                  <div  class="f-data">
                        <label>&nbsp;</label>
				<input type="checkbox" name="findMyProducts"<?php if (isset($this->exp['created_user_id'])) : ?> checked="checked"<?php endif; ?> value="1" /> <?php echo $this->translate('product_list_search_my_products'); ?>
                  </div>
                  <div class="f-submit">
                      <input type="submit" class="button" value="<?php echo $this->translate('product_list_search_button'); ?>" />
                  </div>
              <div class="f-status">
                    <ul>
                          <li<?php if (!isset($this->exp['status'])) : ?> class="f-s-actived"<?php endif; ?>>
                                <a href="<?php echo $this->linkFilter($this->exp); ?>"><?php echo $this->translator('product_list_filter_all'); ?></a>
                          </li>
                          <li<?php if (isset($this->exp['status']) && $this->exp['status'] == 'actived') : ?> class="f-s-actived"<?php endif; ?>>
                                <a href="<?php echo $this->linkFilter($this->exp, 'actived'); ?>"><?php echo $this->translator('product_list_filter_actived'); ?></a>
                          </li>
                          <li<?php if (isset($this->exp['status']) && $this->exp['status'] == 'inactived') : ?> class="f-s-actived"<?php endif; ?>>
                                <a href="<?php echo $this->linkFilter($this->exp, 'inactived'); ?>"><?php echo $this->translator('product_list_filter_inactived'); ?></a>
                          </li>
                          <li<?php if (isset($this->exp['status']) && $this->exp['status'] == 'drafted') : ?> class="f-s-actived"<?php endif; ?>>
                                <a href="<?php echo $this->linkFilter($this->exp, 'drafted'); ?>"><?php echo $this->translator('product_list_filter_drafted'); ?></a>
                          </li>
                          <li<?php if (isset($this->exp['status']) && $this->exp['status'] == 'deleted') : ?> class="f-s-actived"<?php endif; ?>>
                                <a href="<?php echo $this->linkFilter($this->exp, 'deleted'); ?>"><?php echo $this->translator('product_list_filter_trash'); ?></a>
                          </li>
                    </ul>
                </div>
          </div>

    <table class="list" cellspacing="0">
        <thead>
            <tr>
                <td width="3%">
                    <input type="checkbox" class="chkAll"/>
                </td>
                <td width="35%">
                    <?php echo $this->translate('product_name'); ?>
                </td>
                <td width="15%">
                    <?php echo $this->translate('product_category'); ?>
                </td>
                <td width="15%">
                    <?php echo $this->translate('product_status'); ?>
                </td>
                <td width="10%">
                    <?php echo $this->translate('product_created_date'); ?>
                </td>
                <td width="5%">
                    <?php echo $this->languageSwitcher('flag', str_repeat('&nbsp', 2)); ?>
                </td>
                <td width="15%">
                    <?php echo $this->translate('product_actions'); ?>
                </td>
            </tr>
        </thead>
        <tbody>
            <?php
                    if (count($this->products) == 0) {
            ?>
                        <tr>
                            <td colspan="7" style="color: #fc0202;">
                    <?php echo $this->translate('product_list_not_found'); ?>
                    </td>
                </tr>
            <?php
                    } else {
                        $count = count($this->products);
                        $i = 0;
                        foreach ($this->products as $product) {
            ?>
                            <tr id="product_<?php echo $product->id ?>" <?php if ($i == $count - 1)
                                echo 'class="last"'; ?>>
                                <td>
                                    <input type="checkbox" class="chkId" value = "<?php echo $product->id; ?>"/>
                                </td>
                                <td>
                                    <a href="<?php echo $this->url(array('id' => $product->id), 'product_product_edit'); ?>"><?php echo $product->name ?></a>

                                </td>
                                <td><?php echo $product->category_name; ?></td>
                                <td><?php echo $this->translate('product_list_status_' . $product->status);?></td>
                                <td>
                                    <?php echo date('d-m-Y', strtotime($product->created_date)); ?>
                                </td>
                        <td>
                            <?php if (($translations = $this->translationItems($product)) != null) : ?>
                                    <?php foreach ($translations as $language => $translation) : ?>
                                      <?php if ($translation == null && $language != $product->language) : ?>
                                            <?php if (isset($translations[$this->APP_DEFAULT_LANG])) : ?>

                                            <a class="a_helper_action_add" href="<?php echo $this->url(array('source_id' => $translations[$this->APP_DEFAULT_LANG]->id), 'product_product_add_localization'); ?>/<?php echo $language; ?>/">&nbsp;</a>
                                            <?php else :?>
                                            <a class="a_helper_action_add" href="<?php echo $this->url(array(), 'product_product_add'); ?>/<?php echo $language; ?>/">&nbsp;</a>
                                            <?php endif; ?>

                                    <?php elseif (($translation == null && $language == $product->language) || ($translation != null && $translation->id == $product->id)) : ?>
                                    <a class="a_helper_action_edit" href="<?php echo $this->url(array('id' => $product->id), 'product_product_edit'); ?>/<?php echo $language; ?>/">&nbsp;</a>

                                    <?php elseif ($translation != null && $translation->id != $product->id) : ?>
                                    <a class="a_helper_action_edit" href="<?php echo $this->url(array('id' => $translation->id), 'product_product_edit'); ?>/<?php echo $language; ?>/">&nbsp;</a>
                                        <?php endif; ?>
                                <?php endforeach; ?>
                            <?php else : ?>
                                &nbsp;
                            <?php endif; ?>
                        </td>
                        <td>
                    <?php
                            if ($this->allow('edit')) {
                    ?>
                                <a href="<?php echo $this->url(array('id' => $product->id), 'product_product_edit'); ?>"><?php echo $this->translate('product_list_action_edit') ?></a>
                    <?php
                            }
                            if ($this->allow('activate')) {
                                if ($product->status != 'actived') {
                    ?>
                                    | <a href="<?php echo $this->url(array('id' => $product->id), 'product_product_activate'); ?>"><?php echo $this->translate('product_list_action_activate') ?></a>
                    <?php
                                } else {
                    ?>
                                    | <a href="<?php echo $this->url(array('id' => $product->id), 'product_product_activate'); ?>"><?php echo $this->translate('product_list_action_deactivate') ?></a>
                    <?php
                                }
                            }
                            if ($this->allow('delete')) {
                    ?>
                                |  <a class="deleteAction" href="#" rel="<?php echo $product->id; ?>"><?php echo $this->translate('product_list_action_delete') ?></a>
                    <?php
                            }
                    ?>
                        </td>
                    </tr>
            <?php
                            $i++;
                        }
                    } ?>
                </tbody>
                <tfoot>
                    <tr>
                        <td>
                            <input type="checkbox" class="chkAll"/>
                        </td>
                        <td>
                    <?php echo $this->translate('product_name'); ?>
                </td>
                <td>
                    <?php echo $this->translate('product_category'); ?>
                </td>
                <td>
                    <?php echo $this->translate('product_status'); ?>
                </td>
                <td>
                    <?php echo $this->translate('product_created_date'); ?>
                </td>
                <td>
                    <?php echo $this->languageSwitcher('flag', str_repeat('&nbsp', 2)); ?>
                </td>
                <td>
                    <?php echo $this->translate('product_actions'); ?>
                </td>
            </tr>
        </tfoot>
    </table>
    </form>
    <div id="pagination">
        <?php echo $this->paginator()->slide($this->paginator, $this->paginatorOptions); ?>
    </div>
</div>

